<?php
Class  Model 
{
	public $db='';
	private $field='';
	private $page='';
	function __construct()
	{
		$this->dbinit();
		
		
	}
	
	public function dbinit()
	{
	   static $_db;
	   if(empty($_db))
	   {
		$this->db = $_db= new mysqlDB(C('db_host'), C('db_user'), C('db_pass'), C('db_name'));
		//$db_host = $db_user = $db_pass = $db_name = NULL;
	   }
	   else 
	   {
	   $this->db=$_db;
	   }
		
	}
	public function getCfg($key='')
	{
		if($key)
		{
			$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='".$key."'";
			$arr=$this->getOne($sql);
				
		}
		else
		{
			$sql="SELECT op_value,op_key FROM ".$this->table('options')." WHERE op_key='wap_support' ";
			$arr=array();
			$res=$this->getAll($sql);
			if(count($res)>0)
			{
				foreach($res as $k=>$v)
				{
					$arr[$v['op_key']]=$v['op_value'];
				}
			}
		}
		C($arr);
		return $arr;
	}
	public function getAccessToken()
	{
		
		$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='access_token' OR op_key='token_life' OR op_key='appid' OR op_key='appsecret'";
		$temp=$this->getCol($sql);
		$app['appid']=$temp[0];
		$app['appsecret']=$temp[1];
		$wx_token['access_token']=$temp[2];
		$wx_token['token_life']=$temp[3];
		
		
		
		if($wx_token['access_token']==''||$_SERVER['REQUEST_TIME']>$wx_token['token_life']+6438)//7200
		{
	        $new_token=Ctrl("weixin")->getToken($app['appid'], $app['appsecret']);
	     
	        $sql="UPDATE ".$this->table('options')." SET op_value='".$new_token."' WHERE op_key='access_token'";
	       $res=$this->db->query($sql);
	       $sql="UPDATE ".$this->table('options')." SET op_value='".$_SERVER['REQUEST_TIME']."' WHERE op_key='token_life'";
	       $res2=$this->db->query($sql);
	       return $new_token;
		}
		else 
		{
			
		   return $wx_token['access_token'];
		}
	}
	function del($cfg=array())
	{
		$t=$cfg['table'];
		unset($cfg['table']);
		$where=" WHERE 1=1 AND ";
		$logic=" AND ";
		foreach($cfg as $k=>$v)
		{
		   if(is_array($v))
			{
				$where.="".$k." in (".implode(',',$v).")".$logic;
			}
			else 
			{
			     $where.=" ".$k."='".$v."'".$logic;
			}
		}
		
		$where=$where."1=1";
		
		
		$sql="DELETE FROM ".$this->table($t).$where;
		$res=$this->query($sql);
		
		return $res;
	}
	public function update($info,$cfg)
	{
		if($cfg['id'])
		{
		
			foreach($info as $k=>$v)
			{
		
				$where.=" $k='$v',";
		
		
			}
				
			$where=substr($where,0,strlen($where)-1);
		
			$sql="UPDATE ".$this->table($cfg['table'])." SET ".$where." WHERE id='".$cfg['id']."'";
				
			return $res=$this->query($sql);
		}
	}
	public function is_only($val,$cfg=array())
	{
		$sql=' SELECT 1 FROM '.$this->table($cfg['table'])." WHERE ".$cfg['field']." = '".$val."'";
		if($this->getOne($sql))
		{
			return false;
		}
		else 
		{
			return true;
		}
	}
	public function insert($info,$table)
	{
		$_value='';
		foreach($info as $k=>$v)
		{
			if(is_array($v))
			{
				$field='';
				$value='';
				foreach($v as $kk=>$vv)
				{
					$field.="$kk,";
					
					
					$value.="'$vv',";
				}
				$_field=substr($field,0,strlen($field)-1);
				$value=substr($value,0,strlen($value)-1);
				$_value.="(".$value."),";
			}
			else
			{
				$field.="$k,";
		
		
				$value.="'$v',";
					
			}
			
		}
			
			
		$field=substr($field,0,strlen($field)-1);
		$value=substr($value,0,strlen($value)-1);
			if($_value!='')
			{
				$_value=substr($_value,0,strlen($_value)-1);
			   $sql="INSERT INTO ".$this->table($table)." (".$_field.") VALUES ".$_value."";	
			}
			else 
			{
		       $sql="INSERT INTO ".$this->table($table)." (".$field.") VALUES(".$value.")";
			}
		if(!$res=$this->query($sql))
		{
			return false;
		}
		else 
		{
			return $this->insert_id();
		}
	}
	//返回插入的id
	public function insert_id()
	{
		return $this->db->insert_id();
	}
	public function query($sql)
	{
		return $this->db->query($sql);
	}
	public function create_link($page_count,$curpage=1)
	{
		
		return $this->db->create_link($page_count,$curpage);
	}
	
	public function getpagelink()
	{
		return $this->db->page_link;
	}
	public function show_page($sql,$page=1,$page_size=10,$cfg=array(),$count_sql='')
	{
	
		$count_sql=$count_sql==''?'SELECT count(*) ':$count_sql;
		$from_index=strripos($sql,'FROM');
		$c_sql=$count_sql.substr($sql,$from_index);
		$data=array();
		$num=$this->db->getOne($c_sql);
		if($num>0)
		{
			if(GROUP_NAME=='Home')
			{
				$cfg['rewrite']=REWRITE;
			}
			$page=new Page($page,$num,$page_size,$cfg);
			if(GROUP_NAME=='Home'&&DEFAULT_LANG=='en_us')
			{
				$page->set_lang(array('first_page'=>'First','next_page'=>'Next','pre_page'=>'Pre','last_page'=>'Last',));
			}
			
			
			
			
			$page->create_arr();
			$sql=$sql.' LIMIT '.$page->select_from.$page->select_limit;
			$data['result']=$this->db->getAll($sql);
			$data['page']=$page->html_page();
		}
		return $data;
	}
	public function get_page($field,$from,$where='',$page=1,$page_size='10')
	{
		
		return $this->db->get_page($field,$from,$where,$page,$page_size);
       
	}
	
	public function table($table_name)
	{
		
		$table=C('prefix').$table_name;
		
		return $table ;
		
	}
	
	public function getOne($sql)
	{
		$result=$this->db->getOne($sql);
		return $result;
	}
	
	public function getAll($sql)
	{
		$result=$this->db->getAll($sql);
		return $result;
	}
	public function getCol($sql)
	{
		$result=$this->db->getCol($sql);
		return $result;
	}
	public function getRow($sql)
	{
		$result=$this->db->getRow($sql);
		return $result;
	}
	
}